package com.sixteen.ordering_system.component;

import com.sixteen.ordering_system.pojo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        SysUser loginUser = (SysUser) request.getSession().getAttribute("loginUser");

        if (loginUser == null){
            request.setAttribute("msg","您没有权限进行此操作,请先登录！");
            response.sendRedirect("login");
            return false;
        }else {
            if (loginUser.getRole().equals("0") && request.getRequestURL().toString().contains("bus")){
                response.sendRedirect("index");
                return false;
            }
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("postHandle执行{}",modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion执行异常{}",ex);
    }
}
